package com.zc.mychart.chart;

import android.content.Context;
import androidx.core.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.zc.mychart.R;
import com.zc.mychart.model.EntryData;
import com.zc.mychart.utils.ArithUtil;

/**
 * Created by zhangchi on 2018/3/7.
 */

public class KMarkerViewRightY extends MarkerView {
    private Context mContext;
    private MPPointF mOffset = new MPPointF();
    private View rl_border;
    private TextView tv_time;
    private TextView tv_open;
    private TextView tv_close;
    private TextView tv_high;
    private TextView tv_low;
    private TextView tv_change;
    private TextView tv_change_profit;


    private TextView tv_open_text;
    private TextView tv_close_text;
    private TextView tv_high_text;
    private TextView tv_low_text;
    private TextView tv_change_text;
    private TextView tv_change_profit_text;

    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    public KMarkerViewRightY(Context context, int layoutResource) {
        super(context, layoutResource);
        mContext = context;
        rl_border = findViewById(R.id.rl_border);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_open = (TextView) findViewById(R.id.tv_open);
        tv_close = (TextView) findViewById(R.id.tv_close);
        tv_high = (TextView) findViewById(R.id.tv_high);
        tv_low = (TextView) findViewById(R.id.tv_low);
        tv_change = (TextView) findViewById(R.id.tv_change);
        tv_change_profit = (TextView) findViewById(R.id.tv_change_profit);

        tv_open_text = (TextView) findViewById(R.id.tv_open_text);
        tv_close_text = (TextView) findViewById(R.id.tv_close_text);
        tv_high_text = (TextView) findViewById(R.id.tv_high_text);
        tv_low_text = (TextView) findViewById(R.id.tv_low_text);
        tv_change_text = (TextView) findViewById(R.id.tv_change_text);
        tv_change_profit_text = (TextView) findViewById(R.id.tv_change_profit_text);
    }

    public void setMarkerBg(int resId) {
        rl_border.setBackgroundColor(resId);
    }

    public void setTitleColor(int color){
        tv_time.setTextColor(color);
        tv_open_text.setTextColor(color);
        tv_close_text.setTextColor(color);
        tv_high_text.setTextColor(color);
        tv_low_text.setTextColor(color);
        tv_change_text.setTextColor(color);
        tv_change_profit_text.setTextColor(color);
    }

    public void setContentColor(int color){
        tv_open.setTextColor(color);
        tv_close.setTextColor(color);
    }

    public void refreshContent(Entry e, Highlight highlight, float beforeClose) {
        CandleEntry candle = (CandleEntry) e;
        String time = ((EntryData)candle.getData()).time;
        if(TextUtils.isEmpty(time)){
            tv_time.setVisibility(GONE);
        }else{
            tv_time.setVisibility(VISIBLE);
            tv_time.setText(time);
        }
        tv_open.setText(candle.getOpen() + "");
        tv_close.setText(candle.getClose() + "");
        tv_high.setText(candle.getHigh() + "");
        tv_low.setText(candle.getLow() + "");
        int textColor = ContextCompat.getColor(mContext, R.color.mychart_fall_green);
        float change = candle.getClose() - candle.getOpen();
        if (change >= 0) {
            textColor = ContextCompat.getColor(mContext, R.color.mychart_rose_red);
        }
        tv_change.setTextColor(textColor);
        tv_change_profit.setTextColor(textColor);
        String profit = ArithUtil.formatFourDigit(ArithUtil.subFloat(candle.getClose(),beforeClose));;
        tv_change.setText(profit);
        tv_change_profit.setText(ArithUtil.div(Double.valueOf(profit) * 100, beforeClose, 2) + "%");
        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffsetForDrawingAtPoint(float posX, float posY) {
        Chart chart = getChartView();
        if (chart == null)
            return mOffset;
        float width = getWidth();
        if (posX <= chart.getWidth() / 2) {
            mOffset.x = chart.getWidth() - posX - width;
        } else {
            mOffset.x = -posX;
        }
        mOffset.y = -posY;
        return mOffset;
    }
}
